const https = require('../../../utils/https.js');

Page({
    data: {
        value: '',
        keyword: '',
        resultsList: [],
        pageNum: 1,
        pageSize: 10,
        loading: false,
        noMore: false,
        activeTab: 'default',
    },

    goBack() {
        wx.navigateBack();
    },

    onLoad(options) {
        const keyword = options.keyword || '';
        this.setData({
            keyword,
            value: keyword
        });

        if (keyword) {
            this.reportSearch(keyword);
            this.fetchResults(true);
        }
    },

    onSearch(e) {
        const keyword = (e.detail || '').trim();
        if (!keyword) return;

        this.setData({
            keyword,
            value: keyword,
        });
        this.reportSearch(keyword);
        this.fetchResults(true);
    },

    reportSearch(keyword) {
        try {
            https.POST('/search/searchhot/report', {
                keyword
            }, () => {}, () => {});
        } catch (err) {}
    },

    fetchResults(reset = false) {
        if (this.data.loading) return;
        if (reset) {
            this.setData({
                pageNum: 1,
                resultsList: [],
                noMore: false
            });
        }

        const pageNum = this.data.pageNum;
        const pageSize = this.data.pageSize;
        const name = this.data.keyword;

        if (!name) {
            this.setData({
                resultsList: []
            });
            return;
        }

        this.setData({
            loading: true
        });

        const params = {
            pageNum,
            pageSize,
            name
        };

        https.GET('/product/productinfo/list', params, (res) => {
            this.setData({
                loading: false
            });

            if (res && res.code === 200) {
                const rows = res.rows || [];
                let newList = (this.data.resultsList || []).concat(rows);

                // 根据 activeTab 做排序
                const tab = this.data.activeTab;
                if (tab === 'sales') {
                    newList.sort((a, b) => b.count - a.count); // 销量降序
                } else if (tab === 'price') {
                    newList.sort((a, b) => a.price - b.price); // 价格升序
                }

                const noMore = rows.length < pageSize;

                this.setData({
                    resultsList: newList,
                    pageNum: pageNum + 1,
                    noMore
                });
            } else {
                console.error('搜索接口返回异常', res && res.msg);
                if (reset) {
                    this.setData({
                        resultsList: []
                    });
                }
            }
        }, (err) => {
            console.error('搜索接口请求失败', err);
            this.setData({
                loading: false
            });
        });
    },

    gotoDetail(e) {
        const id = e.currentTarget.dataset.id;
        if (!id) return;
        wx.navigateTo({
            url: '/index/pages/mzhGoodsDetail/mzhGoodsDetail?id=' + id
        });
    },

    onTabChange(event) {
        const name = event.detail.name;
        this.setData({
            activeTab: name
        });

        // 切换 tab 后重新排序当前列表
        this.fetchResults(true);
    },

    onPullDownRefresh() {
        this.fetchResults(true);
        wx.stopPullDownRefresh();
    },

    onReachBottom() {
        if (this.data.noMore || this.data.loading) return;
        this.fetchResults(false);
    }

});
